package com.yzc.result;

import lombok.Getter;

import java.util.Arrays;

@Getter
public enum ResultCodeEnum {
    /* 成功状态码 */
    SUCCESS(200, "成功"),
    FAIL(201, "失败"),

    /* 邮件错误 */
    EMAIL_SEND_FAIL(301, "邮件发送失败"),
    EMAIL_CHECK_FAIL(302, "邮件验证失败"),

    /* 客户端错误 4xx */
    BAD_REQUEST(400, "请求参数错误"),
    UNAUTHORIZED(401, "未授权访问"),
    FORBIDDEN(403, "无访问权限"),
    NOT_FOUND(404, "资源不存在"),

    /* 服务端错误 5xx */
    SYSTEM_ERROR(500, "系统异常，请稍后重试"),
    SERVICE_UNAVAILABLE(503, "服务不可用"),

    /* 文件上传错误 */
    FILE_NOT_UPLOAD(600,  "文件未上传"),
    FILE_DOWNLOAD_ERROR(601, "文件下载失败"),
    FILE_DELETE_ERROR(602, "文件删除失败"),
    FILE_UPLOAD_FAIL(603, "文件上传失败"),
    Invalid_FAIL(604, "无效文件"),
    FILE_SIZE_IS_ZERO(605, "文件大小为0"),

    /* 业务错误 1000-1999 */
    BUSINESS_ERROR(1000, "业务逻辑异常"),
    USER_NOT_FOUND(1001, "用户不存在"),
    USER_DISABLED(1002, "用户已禁用"),
    USERNAME_OR_PASSWORD_ERROR(1003, "用户名或密码错误"),
    PHONE_CODE_ERROR(1004, "手机验证码不正确"),

    /* 认证授权错误 2000-2999 */
    INVALID_TOKEN(2000, "无效的访问令牌"),
    TOKEN_EXPIRED(2001, "令牌已过期"),
    ACCESS_DENIED(2003, "没有权限访问该资源"),
    INVALID_JWT(2004, "无效的JWT"),
    INVALID_SIGNATURE(2005, "无效的JWT签名");



    private final int code;
    private final String message;

    ResultCodeEnum(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ResultCodeEnum fromCode(int code) {
        return Arrays.stream(values())
                .filter(e -> e.code == code)
                .findFirst()
                .orElse(null);
    }
}
